#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using AbstractClass.Linq;

namespace AbstractClass.Collections.Graph
{
    [Serializable]
    internal sealed class GraphEdgeCollection<T> : List<GraphEdge<T>>
    {
        public bool Contains(GraphNode<T> from, GraphNode<T> to)
        {
            return this.Any(edge => edge.Source.Equals(from) && edge.Destination.Equals(to));
        }

        public bool Contains(T from, T to)
        {
            return this.Any(edge => edge.Source.Value.Equals(from) && edge.Destination.Value.Equals(to));
        }

        public bool Contains(GraphNode<T> from, GraphNode<T> to, int cost)
        {
            return this.Any(edge => edge.Source.Equals(from) && edge.Destination.Equals(to) && edge.Cost == cost);
        }

        public bool Contains(T from, T to, int cost)
        {
            return
                this.Any(
                    edge => edge.Source.Value.Equals(from) && edge.Destination.Value.Equals(to) && edge.Cost == cost);
        }

        public bool Remove(GraphNode<T> from, GraphNode<T> to)
        {
            return this.ReturnFirstOrDefault(edge => edge.Source.Equals(from) && edge.Destination.Equals(to),
                                             Remove);
        }

        public bool Remove(GraphNode<T> from, GraphNode<T> to, int cost)
        {
            return
                this.ReturnFirstOrDefault(
                    edge => edge.Source.Equals(from) && edge.Destination.Equals(to) && edge.Cost == cost,
                    Remove);
        }

        public bool Remove(T from, T to)
        {
            return this.ReturnFirstOrDefault(
                edge => edge.Source.Value.Equals(from) && edge.Destination.Value.Equals(to), Remove);
        }

        public bool Remove(T from, T to, int cost)
        {
            return this.ReturnFirstOrDefault(
                edge => edge.Source.Value.Equals(from) && edge.Destination.Value.Equals(to) && edge.Cost == cost,
                Remove);
        }

        public bool RemoveAll(IEnumerable<GraphEdge<T>> edges)
        {
            return RemoveAll(edges.Contains) > 0;
        }
    }
}